package com.pantuo.sys.filter;

import com.pantuo.sys.dao.StatisticsOneDao;
import com.pantuo.sys.dao.StatisticsThreeDao;
import com.pantuo.sys.dao.StatisticsTwoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 炳秦
 * @Date: 2021/11/04/11:09
 * @Description:对刷卡日志表的删除在过滤器先执行
 */

//@Order(1)
@WebFilter(filterName="deleteFilter", urlPatterns="/dbApi/delById")
public class DeleteObjectFilter implements Filter {


    @Autowired
    private StatisticsTwoDao statisticsTwoDao;
    @Autowired
    private StatisticsThreeDao statisticsThreeDao;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String className = request.getParameter("className");
        if (!"tn_sfzlog".equals(className)){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        String lineName_s = request.getParameter("lineName_s");
        String vehicleNum1_s = request.getParameter("vehicleNum1_s");
        String date_s = request.getParameter("date_s");
        if (lineName_s == null){
            throw new RuntimeException("线路不能为空");
        }
        if (vehicleNum1_s == null){
            throw new RuntimeException("车辆号不能为空");
        }
        if (date_s == null){
            throw new RuntimeException("日期不能为空");
        }
        int count1 = statisticsTwoDao.selectCountByDateAndLineName(date_s, lineName_s);
        if (count1 <= 1){
            //小于等于1到线路统计表将该记录删除
            statisticsTwoDao.deleteByDateAndLineName(date_s, lineName_s);
        }
        int count2 = statisticsThreeDao.selectCountByDateAndVehicleNum1(date_s, vehicleNum1_s);
        if (count2 <= 1){
            //小于等于1到车辆统计表将该记录删除
            statisticsThreeDao.deleteByDateAndVehicleNum1(date_s, vehicleNum1_s);
        }
        System.out.println("删除多滤器执行！");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
